package org.alvin.code.jdbc.aop;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;

// 自定义 PreparedStatement 代理处理器
@Slf4j
public class PreparedStatementProxyHandler implements InvocationHandler {
    private final PreparedStatement target;

    public PreparedStatementProxyHandler(PreparedStatement target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getName().equals("execute") || method.getName().equals("executeQuery") || method.getName().equals("executeUpdate")) {
            // 从 PreparedStatement 获取 SQL 语句
            String sql = target.toString().substring(target.toString().indexOf(":") + 2);
            log.info("Executing SQL: {}" , sql);
        }
        return method.invoke(target, args);
    }
}
